import StageScene from "./StageScene"
import WeaponData from "./WeaponData"
import Turret from "./Turret"
import Tools from "../Tools"

export default class Map extends Laya.Script3D {
  private map: Laya.MeshSprite3D
  private mat: Laya.BlinnPhongMaterial
  private isWeaponOnMap: number
  private weapon: Laya.Sprite3D
  private static buildEffect: Laya.Sprite3D
  onEnable() {
    this.map = this.owner as Laya.MeshSprite3D
    this.mat = this.map.meshRenderer.material as Laya.BlinnPhongMaterial
    this.isWeaponOnMap = 0
  }
  // onMouseDown() {
  //   console.log(this.owner.parent.getChildIndex(this.owner) + 1 + '号地板被点击')
  // }

  onMouseEnter() {
    this.mat.albedoColor = new Laya.Vector4(1, 0, 0, 1)
  }

  onMouseOut() {
    this.mat.albedoColor = new Laya.Vector4(1, 1, 1, 1)
  }

  /** 
   * 添加武器判断
  */
  public addWeapon() {
    let stage = StageScene.instance
    let weaponData = stage.weaponData
    if (weaponData.index === void 0) {
      stage.coinout.text = '未选择武器'
      stage.coinoutAni.play(0, false)
      return
    }
    // 判断是否有
    if (this.isWeaponOnMap == 1 && !WeaponData.prefabs[weaponData.index * 2 + 1]) {
      Laya.Sprite3D.load(weaponData.weaponPath_up, Laya.Handler.create(this, (weapon_up: Laya.Sprite3D) => {
        let rig = weapon_up.getComponent(Laya.Rigidbody3D) as Laya.Rigidbody3D
        rig.collisionGroup = Laya.Physics3DUtils.COLLISIONFILTERGROUP_CUSTOMFILTER7;
        rig.canCollideWith = Laya.Physics3DUtils.COLLISIONFILTERGROUP_CUSTOMFILTER8;
        weapon_up.addComponent(Turret)
        WeaponData.prefabs[weaponData.index * 2 + 1] = weapon_up
        return this.addWeapon()
      }))
    } else if (!WeaponData.prefabs[weaponData.index * 2]) {
      Laya.Sprite3D.load(weaponData.weaponPath, Laya.Handler.create(this, (weapon: Laya.Sprite3D) => {
        weapon.addComponent(Turret)
        let rig = weapon.getComponent(Laya.Rigidbody3D) as Laya.Rigidbody3D
        rig.collisionGroup = Laya.Physics3DUtils.COLLISIONFILTERGROUP_CUSTOMFILTER7;
        rig.canCollideWith = Laya.Physics3DUtils.COLLISIONFILTERGROUP_CUSTOMFILTER8;
        WeaponData.prefabs[weaponData.index * 2] = weapon
        return this.addWeapon()
      }))
    } else {
      if (this.isWeaponOnMap) {
        // 弹出升级弹框 将3d坐标转换为2d
        let posOut = new Laya.Vector3()
        stage.camera.viewport.project(this.map.transform.position, stage.camera.projectionViewMatrix, posOut)
        stage.buildUI.pos(posOut.x, posOut.y)
        stage.buildUI.visible = true
        stage.buildUI.scaleX = stage.buildUI.scaleY = 0.125 * posOut.z + 0.75
        stage.buildAni.wrapMode = Laya.Animation.WRAP_POSITIVE
        stage.buildAni.play(0, false)
        stage.addBuildEvent(this, this.isWeaponOnMap)
        return
      } else {
        // 添加武器
        if (stage.setCoin(weaponData.price)) {
          let weaponPrefabs = WeaponData.prefabs[weaponData.index * 2]
          this.weapon  = Laya.Sprite3D.instantiate(weaponPrefabs, stage.stageScene, false, this.map.transform.position)
          this.isWeaponOnMap = 1
          this.setBuildEffect()
        } else {
          // 金币不足
          stage.coinout.text = '金币不足'
          stage.coinoutAni.play(0, false)
        }
      }
    }
  }

  public levelupTurrret() {
    let stage = StageScene.instance
    let weaponData = stage.weaponData
    // 武器升级
    if (stage.setCoin(weaponData.price_up)) {
      let weaponUpPrefabs = WeaponData.prefabs[weaponData.index * 2 + 1]
      // 拆除旧武器
      Tools.destroy(this.weapon)
      this.weapon = Laya.Sprite3D.instantiate(weaponUpPrefabs, stage.stageScene, false, this.map.transform.position)
      this.isWeaponOnMap = 2
      this.setBuildEffect()
    } else {
      // 金币不足
      stage.coinout.text = '金币不足'
      stage.coinoutAni.play(0, false)
    }
  }

  public removeTurret() {
    Tools.destroy(this.weapon)
    this.isWeaponOnMap = 0
  }

  private setBuildEffect() {
    if (!Map.buildEffect) {
      Laya.Sprite3D.load('res/atlas/Models/Conventional/BulletBoom.lh', Laya.Handler.create(this, (effect: Laya.Sprite3D) => {
        Map.buildEffect = effect
        this.setBuildEffect()
      }))
    } else {
      let effect = Laya.Sprite3D.instantiate(Map.buildEffect, StageScene.instance.stageScene, false, this.map.transform.position)
      Tools.destroy(effect, 300)
    }
  }
}